const loadGruntPlugins = require('load-grunt-tasks')
const sass = require('sass')
// const browser = require('grunt-browser-sync')
// const { config } = require('grunt')
// 填充数据
let config = {
    data: {
        menus: [{
            name: '首页',
            link: 'index.html'
        }, {
            name: '关于我们',
            link: 'about.html'
        }],
        pkg: require('./package.json'),
        date: new Date()
    }
}
module.exports = grunt => {
    // 自动加载所有grunt插件
    loadGruntPlugins(grunt)
    grunt.initConfig({
        sass: {
            options: {
                implementation: sass
            },
            main: {
                files: {
                    'temp/assets/styles/main.css': 'src/assets/styles/main.scss'
                }
            }
        },
        // 处理js
        babel: {
            options: {
                presets: ['@babel/preset-env']
            },
            main: {
                files: {
                    'temp/assets/scripts/main.js': 'src/assets/scripts/main.js'
                }
            }
        },
        // 处理html
        web_swig: {
            options: {
              swigOptions:{
                cache: false
              },
              getData: function(tpl){
                  return config.data
              }
            },
            your_target: {
                files: {
                    'temp/index.html': 'src/index.html',
                    'temp/about.html': 'src/about.html'
                }
            },
        },

        // 处理image
        imagemin: {
            /* 压缩图片大小 */
            dist: {
                options: {
                    optimizationLevel: 1 //定义 PNG 图片优化水平
                },
                files: [{
                    expand: true,
                    cwd: 'src/assets/images/',//原图存放的文件夹
                    src: ['src/assets/images/*.{png,jpg,jpeg,gif}'], // 优化 img 目录下所有 png/jpg/jpeg/gif图片
                    dest: 'dist/assets/images/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            },
            // 字体文件
            fonts: {
                options: {
                    optimizationLevel: 1 //定义 PNG 图片优化水平
                },
                files: [{
                    expand: true,
                    cwd: 'src/assets/fonts/',//原图存放的文件夹
                    src: ['src/assets/fonts/**'], // 优化 img 目录下所有 png/jpg/jpeg/gif图片
                    dest: 'dist/assets/fonts/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            }
        },        
        // 文件监听
        watch: {
            js: {
                files: ['src/assets/scripts/*.js'],
                tasks: ['babel'] 
            },
            css: {
                files: ['src/assets/styles/*.scss'],
                tasks: ['sass'] 
            },
            swig: {
                files: ['src/*.html'],
                tasks: ['web_swig'] 
            },
            // image: {
            //     files: ['src/assets/images/**'],
            //     tasks: [browserSync.reload()] 
            // }
        },
        // useref处理hmtl文件
        useref: {
            // specify which files contain the build blocks
            html: 'temp/*.html',
            // explicitly specify the temp directory you are working in
            // this is the the base of your links ( "/" )
            temp: 'dist'
        },
        // 压缩js
        uglify: {
            options: {
            },
            dist: {
                files: {
                    'dist/assets/scripts/main.js': [
                        'temp/assets/scripts/main.js',
                        'temp/assets/scripts/vendor.js'
                    ]
                }
            }
        },
        // 压缩css
        cssmin: {
            options: {
                keepSpecialComments: 0
            },
            compress: {
                files: {
                    'dist/assets/styles/main.css': [
                        "temp/assets/styles/main.css",
                        "temp/assets/styles/vendor.css"
                    ]
                }
            }
        },
        // 开发服务器
        browserSync: {
            dev: {
                bsFiles: {
                    src: [
                        'temp/*.html',
                        'temp/assets/styles/*.css',
                        'temp/assets/scripts/*.js',
                        'src/assets/images/**',
                        'src/assets/fonts/**'
                    ]
                },
                options: {
                    watchTask: true,
                    server: {
                        baseDir: ['./temp', 'src', 'public'],
                        routes: {
                            "/node_modules": "node_modules"
                        }
                    }
                }
            },
            watch: {

            }
        }
    })
    grunt.registerTask('dev', ['web_swig', 'sass', 'babel', 'browserSync', 'watch'])
    grunt.registerTask('build', ['dev', 'useref', 'cssmin', 'uglify', 'imagemin'])

}